package com.cloud.hotline.api.service.impl;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.cloud.hotline.api.bean.HotlineRecordSearchCriteria;
import com.cloud.hotline.api.entity.ToHotlineRecord;
import com.cloud.hotline.api.mapper.ToHotlineRecordMapper;
import com.cloud.hotline.api.service.HotlineSearchService;
import com.cloud.hotline.common.utils.DateHelper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import tk.mybatis.mapper.entity.Example;

@Service
public class HotlineSearchServiceImpl implements HotlineSearchService {

	@Autowired
	private ToHotlineRecordMapper toHotlineRecordMapper;

	@Override
	public PageInfo<ToHotlineRecord> searchPage(HotlineRecordSearchCriteria criteria) {
		return PageHelper.startPage(criteria.getPageNum(), criteria.getPageSize()).doSelectPageInfo(() -> {
			Example example = new Example(ToHotlineRecord.class);
			Example.Criteria c = example.createCriteria();
			// 事件名称
			if (!StringUtils.isEmpty(criteria.getName())) {
				c.andLike("recordTypeName", "%" + criteria.getName() + "%");
			}
			// 派单时间
			if (null != criteria.getDispatchDate() && criteria.getDispatchDate().length == 2) {
				c.andGreaterThan("dispatchDate",
						DateHelper.strToFormatDate(criteria.getDispatchDate()[0], "yyyy-MM-dd HH:mm"));
				c.andLessThan("dispatchDate",
						DateHelper.strToFormatDate(criteria.getDispatchDate()[1], "yyyy-MM-dd HH:mm"));
			}
			// 回访时间
			if (null != criteria.getRevisitDate() && criteria.getRevisitDate().length == 2) {
				c.andGreaterThan("revisitDate",
						DateHelper.strToFormatDate(criteria.getRevisitDate()[0], "yyyy-MM-dd HH:mm"));
				c.andLessThan("revisitDate",
						DateHelper.strToFormatDate(criteria.getRevisitDate()[1], "yyyy-MM-dd HH:mm"));
			}
			// 单位
			if (!StringUtils.isEmpty(criteria.getUnitName())) {
				c.andEqualTo("unitName", criteria.getUnitName());
			}
			// 诉求三率： 街道 or 部门
			if (criteria.getKhType() > 0) {
				c.andEqualTo("unitType", criteria.getKhType() - 1);
			}
			// 派遣方式
			if (!StringUtils.isEmpty(criteria.getDispatchType())) {
				c.andEqualTo("dispatchType", criteria.getDispatchType());
			}
			// 市级回访
			if (!StringUtils.isEmpty(criteria.getCityRevisitStateName())) {
				c.andEqualTo("dataTypeId", 1);
				c.andEqualTo("answerFlagName", criteria.getCityRevisitStateName());
			}
			if (!StringUtils.isEmpty(criteria.getCityPleasedFlagName())) {
				c.andEqualTo("dataTypeId", 1);
				c.andEqualTo("pleasedFlagName", criteria.getCityPleasedFlagName());
			}
			if (!StringUtils.isEmpty(criteria.getCityFinishFlagName())) {
				c.andEqualTo("dataTypeId", 1);
				c.andEqualTo("finishFlagName", criteria.getCityFinishFlagName());
			}

			// 区级回访
			if (!StringUtils.isEmpty(criteria.getRevisitStateName())) {
				c.andEqualTo("dataTypeId", 2);
				c.andEqualTo("answerFlagName", criteria.getRevisitStateName());
			}
			if (!StringUtils.isEmpty(criteria.getPleasedFlagName())) {
				c.andEqualTo("dataTypeId", 2);
				c.andEqualTo("pleasedFlagName", criteria.getPleasedFlagName());
			}
			if (!StringUtils.isEmpty(criteria.getFinishedFlagName())) {
				c.andEqualTo("dataTypeId", 2);
				c.andEqualTo("finishFlagName", criteria.getFinishedFlagName());
			}
			c.andEqualTo("deleteFlag", 0);
			toHotlineRecordMapper.selectByExample(example);
		});
	}

}
